package com.imti.secondmenudemo;

import android.widget.LinearLayout;
import androidx.annotation.ArrayRes;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.imti.secondmenudemo.LeftAdapter.onLeftItemClickListener;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    private void initView()
    {
        RecyclerView categoryMenu = findViewById(R.id.menu_category_list);
        RecyclerView deptMenu = findViewById(R.id.menu_dept_list);

        categoryMenu.setLayoutManager(new LinearLayoutManager(this));
        deptMenu.setLayoutManager(new LinearLayoutManager(this));

        List<LeftModel> leftData = new ArrayList<>();
        List<RightModel> rightData = new ArrayList<>();

        String[] categoryList =
            getResources().getStringArray(R.array.category_list);
        for (int i = 0; i < categoryList.length; i++)
        {
            LeftModel model = new LeftModel();
            model.setId(i);
            model.setName(categoryList[i]);

            model.setSelect(i == 0);

            leftData.add(model);
        }

        String[] nkList =
            getResources().getStringArray(R.array.nk_list);
        for (int i = 0; i < nkList.length; i++)
        {
            RightModel model = new RightModel();
            model.setName(nkList[i]);

            rightData.add(model);
        }

        LeftAdapter leftAdapter = new LeftAdapter(this);
        RightAdapter rightAdapter = new RightAdapter(this);

        leftAdapter.setListener(new onLeftItemClickListener()
        {
            @Override
            public void onLeftItemClick(int position)
            {
                for (int i = 0; i < leftData.size(); i++)
                {
                    leftData.get(i).setSelect(false);
                }

                leftData.get(position).setSelect(true);

                leftAdapter.notifyDataSetChanged();

                switch (position)
                {
                    case 0:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.nk_list));
                        break;

                    case 1:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.wk_list));
                        break;

                    case 2:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.fk_list));
                        break;

                    case 3:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.ek_list));
                        break;

                    case 4:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.kfll_list));
                        break;

                    case 5:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.wgk_list));
                        break;

                    case 6:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.qtxt_list));
                        break;

                    case 7:
                        rightData.clear();
                        rightData.addAll(getResToList(R.array.fr_list));
                        break;
                }

                rightAdapter.notifyDataSetChanged();

            }
        });

        leftAdapter.setLeftData(leftData);
        rightAdapter.setRightData(rightData);

        categoryMenu.setAdapter(leftAdapter);
        deptMenu.setAdapter(rightAdapter);
    }

    private List<RightModel> getResToList(@ArrayRes int arrayRes)
    {
        String[] depts = getResources().getStringArray(arrayRes);

        List<RightModel> result = new ArrayList<>();
        for (int i = 0; i < depts.length; i++)
        {
            RightModel model = new RightModel();
            model.setName(depts[i]);

            result.add(model);
        }

        return result;
    }
}